package com.example.comtroller;

import com.example.dao.ExceptionTest;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2016/9/26/026.
 */
@Controller
public class TestSpringException {
//    @ExceptionHandler
//    public ModelAndView testHandlerException(Exception ex){
//        ModelAndView mv=new ModelAndView("error");
//        mv.addObject("exception",ex);
//        return mv;
//    }
    @RequestMapping("testExceptionHandler")
    public String testHandlerException(@RequestParam("i") int i){
        System.out.println("result:"+10/i);
//        if(i==10){
//            throw new ExceptionTest();
//        }
//        System.out.println("result :"+(float)(10/i));
        return "error";
    }
    @RequestMapping("testup")
public String update(@RequestParam("file") MultipartFile file) throws IOException {
    if(!file.isEmpty()){
        String filename="C:\\Users\\Administrator\\Desktop";
        file.transferTo(new File(filename));
    }
    return "success";
}
@RequestMapping("testupdown")
public ResponseEntity<byte[]> testdown(@RequestParam("name") String name) throws IOException {
    HttpHeaders httpHeaders=new HttpHeaders();
    httpHeaders.add("content-Disposition","attachment;filename="+name);
    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("C:\\Users\\Administrator\\Desktop\\"+name)),httpHeaders,HttpStatus.OK);
}
}
